* This file creates a census of inpatient crowding that is used to examine heterogeneity across crowding groups in Appendix F

* 1) Make a list of A&e departments in the bunching data
use "$saveddata/data_complete_withcosts.dta", clear
gen one = 1 
gen trust_code = substr(site_code,1,3)
gen year = year(arrivaldate)
gen month = month(arrivaldate)
gen finyear = 2011 if year==2011
replace finyear = 2011 if year==2012 & month<4
replace finyear = 2012 if year==2012 & month>3
replace finyear = 2013 if year==2013 & month<4
collapse (sum) one, by(trust_code finyear)

save "$saveddata/temp_ae_trusts.dta", replace
*/


* 2) Use inpatient data to create data
use "$sourcedata/ip_2011.dta", clear
gen finyear=2011
append using "$sourcedata/ip_2012.dta", force
replace finyear=2012 if finyear==.

* limit to those with A&E data
rename sitetret site_code
gen trust_code = substr(site_code,1,3)
merge m:1 trust_code finyear using "$saveddata/temp_ae_trusts.dta"
keep if _merge==3
drop _merge

bys trust_code: egen countpat = count(finyear)
drop if countpat<365*10*2	// at least 10 admits per day

* remove weird dates
gen admidate = mdy(admidate_month, admidate_day, admidate_year)
gen disdate = mdy(disdate_month, disdate_day, disdate_year)
gen nodisdate=1 if disdate==.

format %td admidate disdate
drop if admidate>disdate
drop if nodisdate

* exclusions: drop small specialties
bys trust_code tretspef: egen countpat_tretspef = count(finyear)
gen tretspef_pc = countpat_tretspef/countpat
keep if tretspef_pc>=0.01

* compute daily census
keep trust_code admidate disdate

qui levelsof trust_code
foreach i in `r(levels)' {

	preserve
	
	keep if trust_code=="`i'"
	
	local start = td(01apr2011)
	local end = td(31mar2013)
	forv x = `start'/`end' {
		gen census`x' = admidate<=`x' & disdate>=`x' & trust_code=="`i'"
		}


	* Just do for ALL SPECIALTIES.
	collapse (sum) census*, by(trust_code)
	reshape long census, i(trust_code) j(date)

	
	save "$temp_inpat/trustcensus_`i'.dta", replace
	
	restore
	}

* append files together 
set more off
clear
local filelist: dir "$temp_inpat" files "trustcensus_*.dta"
foreach i of local filelist {
	append using "$temp_inpat/`i'"
	}
	
* remove first and last month of each dataset (potentially missing stays)
gen month = mofd(date)
bys trust_code: egen minmonth = min(month)
bys trust_code: egen maxmonth = max(month)
drop if month==minmonth | month==maxmonth

* Try alternative percentiles
foreach x in 99 95 90 85 80 75 50{
bys trust_code: egen threshold`x' = pctile(census), p(`x')
gen block`x' = census>threshold`x'
}


set more off

sort trust_code date

* manual exclusions for missing data at dataset-joint
drop if month(date)==3 | month(date)==4

keep trust_code date census block*
sort trust_code date census
format %td date
compress

* Then create within-trust distributions

cap drop block99 - block66

encode trust_code, gen(trust_num)

forval x=1/145{
gen census`x' = census if trust_num==`x'
}

forv x=1/145{
xtile dist50_`x' = census`x', n(50)
xtile dist50nz_`x' = census`x' if census`x'>0, n(50)
}

gen dist50 = .
gen dist50nz = .

forv x=1/145{
replace dist50 = dist50_`x' if trust_num==`x'
replace dist50nz = dist50nz_`x' if trust_num==`x'
}


save "$saveddata/inpat_trustcensus.dta", replace

use "$saveddata/data_complete_withcosts.dta", replace

* Merge crowding data into main dataset
rename arrivaldate date
gen trust_code = substr(site_code,1,3)
cap drop _merge
merge m:1 date trust_code using "$saveddata/inpat_trustcensus.dta", keepusing(dist50)
drop if _merge==2
rename _merge _mergecrowd

set more off
replace d1=40 if d1==.
merge m:1 d1 using "$saveddata/lookup_complexity.dta", keepusing(complex_death)
drop _merge

tostring dist50, gen(dist50_str)
replace dist50_str = "0" + dist50_str if strlen(dist50_str)<2
gen dist50_str1 = substr(dist50_str,1,1)
destring dist50_str1, replace
replace dist50_str1 = dist50_str1 + 1
tostring dist50_str1, replace
replace dist50_str = dist50_str1 + substr(dist50_str,2,1)

egen majorcrowd = concat(complex_death dist50_str), punct("_")

rename majorcrowd d1_crowd

save "$saveddata/data_complete_crowding.dta"
